package cn.ecut.web;

import cn.ecut.pojo.Customer;
import cn.ecut.pojo.Manager;
import cn.ecut.service.ICustomerService;
import cn.ecut.service.IManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

//https://blog.csdn.net/DragonAtiger/article/details/128903390?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-5-128903390-blog-128259159.235^v38^pc_relevant_sort_base3&spm=1001.2101.3001.4242.4&utm_relevant_index=8

//@ResponseBody

//1、当Controller是存在返回页面的控制器时，不可以使用@RestController
//2、当需要同时返回页面和字符串时，需要使用@Controller，在返回字符串的方法上使用@ResponseBody
//3、当不需要返回页面时，可以直接使用@RestController来代替@Controller和@ResponseBody，即可直接返回结果
@Controller(value = "ManagerController")
@RequestMapping("ManagerController")
public class ManagerController {

    @Autowired
    private IManagerService managerService;

    @Autowired
    private ICustomerService customerService;


    @RequestMapping("/hello")
    public String helloSpring() {
        List<Manager> list =  managerService.list();
        return "../static/index";
    }


    @RequestMapping(value = "/login")
    public String login(@RequestParam("username") String username, @RequestParam(value = "password") String password,Model model) {
        System.out.println(username + "\t" + password);
        String queryUsername = managerService.selectUsername(username);
        String queryPassword = managerService.selectPassword(username);
        //判断用户名和密码是否错误！
        if (((queryUsername != null) && (queryUsername.equals(username)))
                && ((queryPassword != null) && (queryPassword.equals(password)))) {
            List<Customer> customers = customerService.list();
            model.addAttribute("customers",customers);
            return "/Manager/Management";
        } else {
            return "../static/index";
        }
    }
}
